package com.itheima.ydd.service.impl;

import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.itheima.ydd.common.BusinessException;
import com.itheima.ydd.common.R;
import com.itheima.ydd.common.ResultData;
import com.itheima.ydd.common.UserHolder;
import com.itheima.ydd.entity.YddUser;
import com.itheima.ydd.mapper.YddUserMapper;
import com.itheima.ydd.service.IYddArticleService;
import com.itheima.ydd.service.IYddUserDataService;
import com.itheima.ydd.service.IYddUserService;
import com.itheima.ydd.utils.JwtUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.DigestUtils;
import org.springframework.util.StringUtils;

import java.util.HashMap;
import java.util.Map;

//import cn.hutool.core.util.StrUtil;
//import com.itheima.ydd.controller.CommonController;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author author
 * @since 2022-05-22
 */
@Service
public class YddUserServiceImpl extends ServiceImpl<YddUserMapper, YddUser> implements IYddUserService {
    @Autowired
    private IYddUserDataService yddUserDataService;
    @Autowired
    private IYddArticleService iYddArticleService;

    /**
     * 修改用户资料
     *
     * @param yddUser
     * @return
     */
    @Override
    public ResultData updateUser(YddUser yddUser) {

        Integer id = yddUser.getId();
        LambdaQueryWrapper<YddUser> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(YddUser::getId, id);
        YddUser one = getOne(wrapper);
              //根据用户要修改的手机号判断

        //如果手机号是本机手机号则修改成功


        String mobile = yddUser.getMobile();
        LambdaQueryWrapper<YddUser> wrapper1 = new LambdaQueryWrapper<>();
        wrapper1.eq(YddUser::getMobile, mobile);
        YddUser one1 = getOne(wrapper1);
        if ((one != null && yddUser.getMobile().equals(one.getMobile())) || (!ObjectUtil.equals(mobile, one.getMobile()) && one1 == null)) {
            updateById(yddUser);
            return ResultData.ok("修改成功");
        } else {
            throw new BusinessException("手机号已被其他用户注册");
        }
    }

    @Override
    public ResultData pageList(String sex, String username, String mobile, String page, String limit) {

//分页构造器

        Page<YddUser> yddUserPage = new Page<YddUser>(Long.valueOf(page), Long.valueOf(limit));
        //构建搜索条件 - name进行模糊匹配
        LambdaQueryWrapper<YddUser> wrapper = new LambdaQueryWrapper<>();
        wrapper.like(!StringUtils.isEmpty(username), YddUser::getUsername, username);
        //构建排序条件 - 更新时间倒序排序
        wrapper.like(!StringUtils.isEmpty(mobile), YddUser::getMobile, mobile);
        wrapper.eq(!StringUtils.isEmpty(sex), YddUser::getSex, sex);
        wrapper.eq(YddUser::getDefaultData, 1);

        Page<YddUser> page2 = page(yddUserPage, wrapper);
        //执行分页查询
        HashMap<Object, Object> map = new HashMap<>();
        map.put("count", page2.getCountId());
        map.put("pageSize", page2.getSize());
        map.put("currentPage", page2.getCurrent());
        map.put("data", page2.getRecords());
        map.put("count", page2.getTotal());

        return ResultData.ok("查询成功", map);


    }

    @Override
    public void delByid(String id) {

        if (id == null) {
            throw new BusinessException("未选中要操作的数据");
        }
        LambdaUpdateWrapper<YddUser> wrapper = new LambdaUpdateWrapper<>();
        wrapper.set(YddUser::getDefaultData, 0).eq(YddUser::getId, Integer.valueOf(id));
        update(wrapper);
    }


    @Override
    public void addUser(YddUser yddUser) {
        String mobile = yddUser.getMobile();
        LambdaQueryWrapper<YddUser> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(YddUser::getMobile, mobile);
        YddUser one = getOne(wrapper);
        if (one != null) {
            throw new BusinessException("该手机号已绑定其他账号");
        } else {
            yddUser.setDefaultData(String.valueOf(1));
            yddUser.setPassword(DigestUtils.md5DigestAsHex(yddUser.getPassword().getBytes()));
            save(yddUser);
        }

    }

    @Override
    public YddUser rowInfo(String id) {
        if (id == null) {
            throw new BusinessException("未选中操作数据");
        }
        return getById(id);
    }

    @Override
    public ResultData getUserInfo() {
        String getUserInfo = UserHolder.getUserId();
        YddUser byId = getById(Integer.valueOf(getUserInfo));
        Integer id = byId.getId();
        //收藏
        Integer collectNum = yddUserDataService.getnum(id, 1);
        //点赞
        Integer zanNum = yddUserDataService.getnum(id, 2);

        Integer getarticle = iYddArticleService.getarticle(id);

        HashMap<Object, Object> map = new HashMap<>();
        map.put("articleNum", getarticle);
        map.put("collectNum", collectNum);
        map.put("zanNum", zanNum);
        map.put("userInfo", byId);
        return ResultData.ok(map);
    }

    @Override
    public ResultData reg(Map<String, String> params) {

        String mobile = params.get("mobile");
        LambdaQueryWrapper<YddUser> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(YddUser::getMobile, mobile);
        YddUser one = getOne(wrapper);

        if (one != null) {
            return ResultData.error("该手机已注册账号,请前往登录");
        } else {
            YddUser yddUser = new YddUser();
            yddUser.setPassword(DigestUtils.md5DigestAsHex(params.get("password").getBytes()));
            yddUser.setMobile(params.get("mobile"));
            yddUser.setSex(1);
            yddUser.setUsername(params.get("mobile"));
            save(yddUser);
            // 生成token信息
            LambdaQueryWrapper<YddUser> wrapper1 = new LambdaQueryWrapper<>();
            wrapper1.eq(YddUser::getMobile, mobile);
            YddUser one1 = getOne(wrapper);

            Map retMap = new HashMap();
            Map tokenMap = new HashMap<>();

            tokenMap.put("id", one1.getId());
            tokenMap.put("mobile", one1.getMobile());

            String token = JwtUtils.getToken(tokenMap);
            retMap.put("token", token);
            return ResultData.ok("token", JwtUtils.getToken(tokenMap));
        }
    }
}